cmake_minimum_required(VERSION 3.10)
project(etcd-node-manager VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置构建类型，如果未指定则默认为Debug而非Release
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()

# 根据构建类型设置编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")

# 查找依赖库
find_package(CURL REQUIRED)
find_package(PkgConfig REQUIRED)

# 更改jsoncpp的查找方式
if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
    # 针对M1 Mac的特殊处理
    execute_process(
        COMMAND brew --prefix jsoncpp
        OUTPUT_VARIABLE JSONCPP_PREFIX
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    
    set(JSONCPP_INCLUDE_DIRS "${JSONCPP_PREFIX}/include")
    set(JSONCPP_LIBRARIES "${JSONCPP_PREFIX}/lib/libjsoncpp.dylib")
    message(STATUS "Using Homebrew jsoncpp: ${JSONCPP_PREFIX}")
else()
    # 常规方式查找
    pkg_check_modules(JSONCPP REQUIRED jsoncpp)
endif()

# 包含头文件目录
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CURL_INCLUDE_DIRS})
include_directories(${JSONCPP_INCLUDE_DIRS})

# 源文件
set(SOURCES
    src/main.cpp
    src/etcd_client.cpp
    src/node_manager.cpp
    src/cli_manager.cpp
    src/base64.cpp
    src/logger.cpp
)

# 可执行文件
add_executable(etcd-node-manager ${SOURCES})

# 链接库
target_link_libraries(etcd-node-manager
    ${CURL_LIBRARIES}
    ${JSONCPP_LIBRARIES}
    pthread
)

# 安装
install(TARGETS etcd-node-manager DESTINATION bin)

# 显示构建配置信息
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "CURL_LIBRARIES: ${CURL_LIBRARIES}")
message(STATUS "JSONCPP_LIBRARIES: ${JSONCPP_LIBRARIES}") 